package com.ymatou.iapi.optrade.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.AuthenticateFeeTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.AuthenticateFreightTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.AuthenticateDeliveryReq;
import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.AuthenticateDeliveryCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceAuthenticateCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 发货 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_AuthenticateDelivery {

	private static AuthenticateDeliveryReq authenticateDeliveryReq;
	private static AuthenticateDeliveryCall authenticateDeliveryCall;
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("鉴定中心发货接口");
	}

	@Before
	public void caseUp() {

		authenticateDeliveryReq = new AuthenticateDeliveryReq();
		authenticateDeliveryCall = new AuthenticateDeliveryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(authenticateDeliveryCall.getOpurl());
		
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	/**
	 * 校验订单表状态，单主状态操作日志表
	 */
	private void verifyOrderStatus(long orderId, Boolean supplementDomestic) {

		// 订单表(order)
		OrderPo ymtOrders = orderShardingWapper.selectOrderbyOrderId(orderId);

		Logger.comment(String.format("验证订单表(order)表 orderId: %s", orderId));

		Logger.verifyEquals(3, ymtOrders.getOrderStatus(), "验证orderStatus");
		Logger.verifyEquals(supplementDomestic, ymtOrders.getSupplementDomestic(),
				"验证supplementDomestic");
		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(ymtOrders.getDeliveryTime()) < 2,
				"验证数据库deliveryTime跟系统当前时间差为2分钟内");
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AuthenticateDelivery_001() {
		Logger.start(true, "先发国际段，再发国内段，鉴定中心发货");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticateAndLogisticsType(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			Thread.sleep(3000);
			
			// 国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, "Y013", 1, true);
			Thread.sleep(3000);
			
			// 国内段补录
			TradingCallService.modifyOrSupplementBillNo(orderId, sellerId, "Y013", 3, true);
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");
			
			authenticateDeliveryReq.setSellerId(sellerId);
			authenticateDeliveryReq.setOrderId(orderId);
			authenticateDeliveryReq.setExpressCompany("sf");
			authenticateDeliveryReq.setYmtExpressCode("Y013");
			authenticateDeliveryReq.setBillNo(TestDataManager.randomStr(12));
			
			authenticateDeliveryCall.setData(authenticateDeliveryReq);
			authenticateDeliveryCall.callService();

			Logger.verifyEquals(true, authenticateDeliveryCall.getSuccess(),
					"验证success");

			// 数据库验证
			verifyOrderStatus(orderId, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AuthenticateDelivery_002() {
		Logger.start(true, "先发国际段，鉴定中心发货");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticateAndLogisticsType(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());

			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			Thread.sleep(3000);

			// 国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, "Y013", 1, true);
			Thread.sleep(5000);

			Logger.comment("前置数据准备结束");

			authenticateDeliveryReq.setSellerId(sellerId);
			authenticateDeliveryReq.setOrderId(orderId);
			authenticateDeliveryReq.setExpressCompany("sf");
			authenticateDeliveryReq.setYmtExpressCode("Y013");
			authenticateDeliveryReq.setBillNo(TestDataManager.randomStr(12));

			authenticateDeliveryCall.setData(authenticateDeliveryReq);
			authenticateDeliveryCall.callService();

			Logger.verifyEquals(true, authenticateDeliveryCall.getSuccess(),
					"验证success");

			// 数据库验证
			verifyOrderStatus(orderId, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
